Imports System.Windows.Forms

Public Class frmFilter

    Public DataGrid As DataGridView

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim FilterString As String = Me.txtFilterString.Text '.Replace("'", "")
        Dim ColName As String = Me.cboColumnName.Text '.Replace(" ", "_")
        If FilterString <> "" Then FilterString &= " AND "
        If Me.txtSearchString.Text.ToUpper.Contains("NULL") Then
            FilterString &= String.Format("{0} {1}", ColName, Me.txtSearchString.Text)
        ElseIf DataGrid.Columns.Item(ColName).ValueType Is GetType(String) Then
            FilterString &= String.Format("{0} LIKE '{1}'", ColName, Me.txtSearchString.Text)
        ElseIf DataGrid.Columns.Item(ColName).ValueType Is GetType(Double) Or DataGrid.Columns.Item(ColName).ValueType Is GetType(Decimal) Then
            'if is non-numeric, assume filter string contains an operator like <, etc.; otherwise assume =
            FilterString &= String.Format("{0} {2} {1}", ColName, Me.txtSearchString.Text, IIf(IsNumeric(Me.txtSearchString.Text), "=", ""))
        ElseIf DataGrid.Columns.Item(ColName).ValueType Is GetType(Date) Then
            'if is non-numeric, assume filter string contains an operator like <, etc.; otherwise assume =
            FilterString &= String.Format("{0} {2} {1}", ColName, Me.txtSearchString.Text, IIf(IsDate(Me.txtSearchString.Text), "=", ""))
        Else 'all other types use =
            FilterString &= String.Format("{0} = {1}", ColName, Me.txtSearchString.Text)
        End If
        Me.txtFilterString.Text = FilterString
        ResetFilter()
    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub frmFilter_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            With DataGrid
                Me.cboColumnName.Items.Clear()
                For Each col As DataGridViewColumn In .Columns
                    'If col.ValueType Is GetType(String) Then Me.cboColumnName.Items.Add(col.Name)
                    Me.cboColumnName.Items.Add(col.Name)
                Next
            End With
            If Me.cboColumnName.Items.Count > 0 Then Me.cboColumnName.SelectedIndex = 0
        Catch ex As Exception
            ErrorMsg(, ex)
        End Try
    End Sub

    Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
        Me.txtFilterString.Text = ""
        ResetFilter()
    End Sub

    Private Sub ResetFilter()
        Dim bs As New BindingSource(DataGrid.DataSource, DataGrid.DataMember)
        Try
            Dim FilterString As String = Me.txtFilterString.Text
            If FilterString = "" Then
                bs.RemoveFilter()
            Else
                bs.RemoveFilter()
                bs.Filter = FilterString
            End If
            DataGrid.DataSource = bs
        Catch ex As Exception
            WarningMsg("An invalid filter expression was encountered:\n\n" & ex.Message)
            bs.RemoveFilter()
        End Try
    End Sub

    Private Sub cboColumnName_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboColumnName.SelectedIndexChanged
        txtSearchString.Text = ""
    End Sub

    Private Sub txtFilterString_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFilterString.Validated
        ResetFilter()
    End Sub

    Private Sub txtSearchString_Validated(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtSearchString.Validated
        txtSearchString.Text = txtSearchString.Text.Replace("'", "")
    End Sub
End Class
